package com.doublefish.doublefishrobot.ui.home.path.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.List;

/**
 * 作者:dugaofeng on 2025/4/9 16:50
 */
public class PathYZView extends View {

    private List<float[]> matrix;
    private float height;


    //手写画笔
    private final Paint mPaint = new Paint();
    //画笔的路径
    private final Path mPath = new Path();


    public PathYZView(Context context) {
        this(context, null);
    }

    public PathYZView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathYZView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PathYZView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        setBackgroundColor(Color.WHITE);
    }

    public void setHeightAndMatrix(float height, List<float[]> matrix) {
        this.height = height;
        this.matrix = matrix;

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (height < 0.000001 || matrix == null ) {
            return;
        }
        //球台尺寸 长274 宽152
        //长度用274为基准
        int viewWidth =  getWidth();
        int viewHeight = getHeight();
        float rate = 100.0f * viewWidth / 274; //与实际的球台的尺寸的倍率

        mPath.reset();
        // 设置为划线模式
        mPaint.setStyle(Paint.Style.STROKE);
        //设置当前画笔粗细
        mPaint.setStrokeWidth(5.0f);

        //画球网
        //设置当前笔头颜色
        mPaint.setColor(Color.BLACK);
        //	球网高度：整个球网的顶端距离比赛台面15.25厘米。
        mPath.moveTo(viewWidth / 2.0f, viewHeight);//将画笔移到某点 (发球口位置)
        mPath.lineTo(viewWidth / 2.0f, viewHeight - 0.1525f * rate);
        canvas.drawPath(mPath, mPaint);

        //发球轨迹
        mPath.reset();
        //设置当前笔头颜色
        mPaint.setColor(Color.GREEN);

        float ballHeight = viewHeight - height * rate;
        //将笔移到某点 (发球口位置)
        mPath.moveTo(0, ballHeight);
        for (int i = 0; i < matrix.size(); i++) {
            float[] point = matrix.get(i);
            mPath.lineTo( point[1] * rate, ballHeight - point[2] * rate);
        }
        canvas.drawPath(mPath, mPaint);

        mPaint.setColor(Color.rgb(1.0f, 0.65f,0.0f));
        mPaint.setStyle(Paint.Style.FILL); // 设置为填充模式
        float[] point = matrix.get(matrix.size() - 1);
        canvas.drawCircle(point[1] * rate, ballHeight - point[2] * rate - 10, 10, mPaint);
    }
}
